// repository/AssetRepository.kt
package com.trumeen.immichgallery.repository

import com.trumeen.immichgallery.model.Asset
import com.trumeen.immichgallery.model.LoginRequest
import com.trumeen.immichgallery.model.LoginResponse
import com.trumeen.immichgallery.model.MemoriesResponseItem
import com.trumeen.immichgallery.model.Person
import com.trumeen.immichgallery.network.ImmichService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

class AssetRepository @Inject constructor(
    private val immichService: ImmichService
) {

    suspend fun getOnThisDayAssets(dataTime: String): Result<List<MemoriesResponseItem>> {
        return withContext(Dispatchers.IO) {
            try {
                val response = immichService.getOnThisDayAssets(dataTime)
                if (response.isSuccessful) {
                    val data = response.body()
                    if (data != null) {
                        Result.success(data)
                    } else {
                        Result.failure(Exception("获取图片失败: 服务器返回空数据"))
                    }
                } else {
                    Result.failure(Exception("获取图片失败: 错误代码 ${response.code()}"))
                }
            } catch (e: Exception) {
                Result.failure(Exception("获取图片失败: ${e.message}"))
            }
        }
    }

    suspend fun getAllPeople(): Result<List<Person>> {
        return withContext(Dispatchers.IO) {
            try {
                val response = immichService.getAllPeople()
                if (response.isSuccessful) {
                    val data = response.body()
                    if (data != null) {
                        Result.success(data.people)
                    } else {
                        Result.failure(Exception("获取人物列表失败: 服务器返回空数据"))
                    }
                } else {
                    Result.failure(Exception("获取人物列表失败: 错误代码 ${response.code()}"))
                }
            } catch (e: Exception) {
                Result.failure(Exception("获取人物列表失败: ${e.message}"))
            }
        }
    }

    suspend fun login(serverAddress: String, email: String, password: String): Result<LoginResponse> {
        return withContext(Dispatchers.IO) {
            try {
                // 这里需要根据服务器地址创建相应的服务实例
                // 简化处理，使用当前服务实例
                val response = immichService.login(LoginRequest(email, password))
                if (response.isSuccessful) {
                    val loginResponse = response.body()
                    if (loginResponse != null) {
                        Result.success(loginResponse)
                    } else {
                        Result.failure(Exception("登录失败: 服务器返回空数据"))
                    }
                } else {
                    Result.failure(Exception("登录失败: 错误代码 ${response.code()}"))
                }
            } catch (e: Exception) {
                Result.failure(Exception("登录失败: ${e.message}"))
            }
        }
    }
}